package com.github.xuemingyu.springboot.acticitycloud.service.impl;

import com.bidanet.bdcms.core.vo.Page;
import com.bidanet.hibernate.lambda.core.LambdaCriteria;
import com.github.xuemingyu.springboot.acticitycloud.dao.ConversationDao;
import com.github.xuemingyu.springboot.acticitycloud.entity.Conversation;
import com.github.xuemingyu.springboot.acticitycloud.service.ConversationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Objects;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: xmy
 * \* Date: 2019/1/29
 * \* Time: 10:57
 * \
 */
@Service
@Transactional
public class ConversationServiceImpl extends BaseServiceImpl<Conversation> implements ConversationService {
    @Autowired
    private ConversationDao conversationDao;

    @Override
    public void queryLike(Conversation conversation, Page<Conversation> page) {
        LambdaCriteria<Conversation> lambdaCriteria = lambdaQuery.create(Conversation.class);
        if (Objects.nonNull(conversation.getActivityId())) {
            lambdaCriteria.eq(q -> {
                q.setActivityId(conversation.getActivityId());
            });
        }
        if (StringUtils.isNotEmpty(conversation.getChatId())) {
            lambdaCriteria.eq(q -> {
                q.setChatId(conversation.getChatId());
            });
        }
        lambdaCriteria.eq(q -> {
            q.setDeleteTag(false);
        });
        List<Conversation> conversations = lambdaCriteria.orderDesc("id").list(page.getPageCurrent(), page.getPageSize());
        Long count = lambdaCriteria.count();
        page.setList(conversations);
        page.setTotal(count);
    }

    @Override
    public void insert(Conversation conversation) {
        checkNull(conversation.getActivityId(), "活动id不能为空");
        checkString(conversation.getChatId(), "会话id不能为空");
        conversation.setDeleteTag(false);
        conversationDao.save(conversation);
    }

    @Override
    public void delete(Long id) {
        Conversation one = conversationDao.findOne(id);
        one.setDeleteTag(true);
        conversationDao.save(one);
    }

    @Override
    protected JpaRepository<Conversation, Long> getDao() {
        return conversationDao;
    }
}
